package com.platform.controller;

import com.platform.entity.DynamicCommentEntity;
import com.platform.service.DynamicCommentService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 评论回复
 *
 */
@RestController
@RequestMapping("dynamiccomment")
public class DynamicCommentController {
    @Autowired
    private DynamicCommentService dynamicCommentService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<DynamicCommentEntity> commentList = dynamicCommentService.queryList(query);
        int total = dynamicCommentService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(commentList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("comment:info")
    public R info(@PathVariable("id") Integer id) {
        DynamicCommentEntity comment = dynamicCommentService.queryObject(id);

        return R.ok().put("comment", comment);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("comment:save")
    public R save(@RequestBody DynamicCommentEntity comment) {
        dynamicCommentService.save(comment);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("comment:update")
    public R update(@RequestBody DynamicCommentEntity comment) {
        dynamicCommentService.update(comment);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("comment:delete")
    public R delete(@RequestBody Integer[] ids) {
        dynamicCommentService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<DynamicCommentEntity> list = dynamicCommentService.queryList(params);

        return R.ok().put("list", list);
    }

    /**
     * 修改状态
     */
    @RequestMapping("/toggleStatus")
    @RequiresPermissions("comment:toggleStatus")
    public R toggleStatus(@RequestBody DynamicCommentEntity comment) {
        dynamicCommentService.toggleStatus(comment);

        return R.ok();
    }

    /**
     * 总计
     */
    @RequestMapping("/queryTotal")
    public R queryTotal(@RequestParam Map<String, Object> params) {
        int sum = dynamicCommentService.queryTotal(params);

        return R.ok().put("sum", sum);
    }
}
